<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace App\Model;

use App\Base\BaseModel;
use App\Entity\GradeCategory;
use Doctrine\Bundle\DoctrineBundle\Registry;
use App\Setting\GradeCategorySetting;
use Symfony\Component\HttpFoundation\Request;

class GradeCategoryModel extends BaseModel{


    /*
     * 列表基本设置【通用模块必须】
     */
    public function listSettingDefault()
    {
        $settings = new GradeCategorySetting($this->getDoctrine());
        return $settings->listConfig();
    }

    //获取所有分类
    public function getAllGradeCategory(){
        $qb = $this->getQueryBuilder();
        $qb->select("*")
            ->from("Grade_Category");
        return $qb->execute()->fetchAll();
    }

    //获取所有分类并构建分类树
    public function getAllGradeCategoryTree()
    {
        $categories = $this->getAllGradeCategory();
        $GradeCategoryTree = $this->buildeCatTreeArray($categories);
        $data = array();
        foreach ($GradeCategoryTree as $class) {
            $prefix = "";
            if ($class["level"] > 0) {
                $prefix .="├";
                for ($i=0;$i<$class["level"];$i++ ) {
                    $prefix .="─";
                }
            }
            $data[] = array(
                "id"=>$class["id"],
                "name"=>$prefix.$class["name"],
            );
        }
        return $data;
    }

    //获取第一层分类
    public function getRootGradeCategory()
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")
            ->where("level = ?")
            ->setParameter(0,"1")
            ->from("Grade_Category");
        return $qb->execute()->fetchAll();
    }

    public function getSons($parent)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")
            ->where("parent = ?")
            ->setParameter(0,$parent)
            ->orderBy("orders")
            ->from("Grade_Category");
        return $qb->execute()->fetchAll();
    }

    public function getSons4Setting($parent)
    {
        $data = $this->getSons($parent);
        $return = array("0"=>"请选择");
        foreach ($data as $row) {
            $return[$row["id"]] = $row["name"];
        }
        return $return ;
    }

    /*
     * 通用Rep
     */
    public function getRep()
    {
        $settings = new GradeCategorySetting($this->getDoctrine());
        return $this->em->getRepository($settings->bundleName.":".$settings->entityName);
    }

    /**
     * @return GradeCategory[]
     */
    public function getAll()
    {
        return $this->getRep()->findAll();
    }

    public function getAllParentSons()
    {
        $data = $this->getAllGradeCategory();

        $re = $this->findSons(0,$data);
        return $re;
    }

    public function findSons($id,$allData)
    {
        $sons = [];
        foreach ($allData as $one) {
            if ($one["parent"] == $id) {
                $one["sons"] = $this->findSons($one["id"],$allData);
                $sons[] = $one;
            }
        }
        return $sons;
    }



}